angular.module('start.user.orderguide.ctrl', [])

  .controller('orderguideCtrl', function($scope, $timeout,$ionicScrollDelegate, $ionicLoading, $rootScope, $location, $$UserOrderService) {
    console.log("orderguideCtrl");

    $scope.tabStatus = 1;

    $scope.guideororders = [];
    $rootScope.orderDetail = {};

    $scope.tabStatusChange = function(index) {
      $scope.tabStatus = index;
      $ionicScrollDelegate.scrollTop(0);
    };


    $scope.getGuideOrdersList = function() {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      // 获取向导的订单列表
      $$UserOrderService.getGuideOrders()
        .then(function(res) {
          $scope.guideororders = res.data.list;
          $ionicLoading.hide();
        });
    };
    $scope.getGuideOrdersList();

    // 向导确认预约
    $scope.guideSureOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.changeOrderState(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $timeout(function() {
            $scope.getGuideOrdersList();
          }, 1500);
        });
    };

    // 向导取消预约
    $scope.cancelOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.guideCancelOrder(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $timeout(function() {
            $scope.getGuideOrdersList();
          }, 1500);
        });
    };

    // 向导删除 订单
    $scope.guidedeleteOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.deleteOrder(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $timeout(function() {
            $scope.getGuideOrdersList();
          }, 1500);
        });
    };

    // 向导确认完成服务  102 -> 103
    $scope.guideSureServiceOrder = function(orderId, state) {
      $ionicLoading.show({
        template: '<ion-spinner icon="ios"></ion-spinner>',
        noBackdrop: true
      });
      $$UserOrderService.changeOrderState(orderId, state)
        .then(function(res) {
          $ionicLoading.show({
            template: res.msg,
            noBackdrop: true,
            duration: 1500
          });
          $timeout(function() {
            $scope.getGuideOrdersList();
          }, 1500);
        });
    };

    // 查看订单详情页面
    $scope.goorderdetailguide = function(index) {
      $rootScope.orderDetail = $scope.guideororders[index];
      console.log($rootScope.orderDetail);
      $location.path("orderdetailguide/"+$rootScope.orderDetail.orderId);
    };

    $scope.calcdateDays = function(start, end) {
      return parseInt((new Date(end).getTime() - new　 Date(start).getTime()) / (86400000));
    };

  });
